/***********************************************************************  
 *   @package：cn.wj.base.util，@class-name：SqlGenerator.java   
 *   @copyright     Copyright:   2015-2016     
 *   @creator       wenjie 
 *   @create-time   2016年1月10日   下午2:14:29   
 *   @revision      $Id: 1.0    
 ***********************************************************************/
package cn.wj.base.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wj.base.annotation.IdMapping;
import cn.wj.base.annotation.TableMapping;

public class SqlGenerator {
	/**
	 * 得到数据库表名
	 */
	public static <T> String getTable(Class<T> entryClass) {
		TableMapping MM = entryClass.getAnnotation(TableMapping.class);
		return MM.table();
	}

	/**
	 * 生成查询字段
	 * 
	 * @param entryClass
	 * @return
	 */
	public static <T> String getField(Class<T> entryClass) {
		Field[] field = entryClass.getDeclaredFields();
		StringBuilder sql = new StringBuilder();
		for (Field f : field) {
			sql.append(",");
			sql.append(f.getName());
		}
		return sql.toString().substring(1);
	}

	/**
	 * 得到根本据住建查询实体的sql
	 * 
	 * @param entryClass
	 * @return
	 */
	public static <T> String getByIdSql(Class<T> entryClass) {
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT ");
		sql.append(SqlGenerator.getField(entryClass));
		sql.append(" FROM ");
		sql.append(SqlGenerator.getTable(entryClass));
		sql.append(" where ");
		sql.append(SqlGenerator.getId(entryClass));
		sql.append(" =? ");
		return sql.toString();
	}

	/**
	 * 查询一个表的结果sql
	 * 
	 * @param entryClass
	 * @return
	 */
	public static <T> String getAllSql(Class<T> entryClass) {
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT ");
		sql.append(SqlGenerator.getField(entryClass));
		sql.append(" FROM ");
		sql.append(SqlGenerator.getTable(entryClass));
		return sql.toString();
	}
	/**
	 * 查询一页的结果sql
	 * 
	 * @param entryClass
	 * @return
	 */
	public static <T> String getPageSql(Class<T> entryClass) {
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT ");
		sql.append(SqlGenerator.getField(entryClass));
		sql.append(" FROM ");
		sql.append(SqlGenerator.getTable(entryClass));
		sql.append(" LIMIT ?,?");
		return sql.toString();
	}
	/**
	 * 得到主键列
	 * 
	 * @param entryClass
	 * @return
	 */
	public static <T> String getId(Class<T> entryClass) {
		Field[] field = entryClass.getDeclaredFields();
		for (Field f : field) {
			if (f.isAnnotationPresent(IdMapping.class)) {
				return f.getName();
			}
		}
		return null;
	}

	/**
	 * 得到主键列和值
	 * 
	 * @param entryClass
	 * @return
	 */
	public static <T> Map<Object, Object> getIdValue(Class<T> entryClass) {
		Field[] field = entryClass.getDeclaredFields();
		Map<Object, Object> map = null;
		for (Field f : field) {
			if (f.isAnnotationPresent(IdMapping.class)) {
				f.setAccessible(true);
				try {
					map = new HashMap<Object, Object>();
					map.put(f.getName(), f.get(entryClass));
				} catch (IllegalArgumentException | IllegalAccessException e) {
					System.out.println("得到实体主键和值时出错误" + e.getMessage());
				}
				break;
			}
		}
		return map;
	}

	/**
	 * 生成插入语句
	 * 
	 * @param entry
	 * @return
	 * @throws MultException
	 */
	public static <T> Map<String, List<Object>> getInsertSql(Object entry) {
		Map<String, List<Object>> map = new HashMap<String, List<Object>>();
		List<Object> list = new ArrayList<Object>();
		Field[] field = entry.getClass().getDeclaredFields();
		StringBuilder sql = new StringBuilder();
		StringBuilder column = new StringBuilder();
		StringBuilder columnValue = new StringBuilder();
		// 是否自增长
		boolean isauto = true;
		try {
			for (Field f : field) {
				if (f.isAnnotationPresent(IdMapping.class)) {
					f.setAccessible(true);
					if (!ObjectUtil.isNull(f.get(entry)))
						isauto = false;
					break;
				}
			}

			if (isauto) {
				for (Field f : field) {
					if (!f.isAnnotationPresent(IdMapping.class)) {
						f.setAccessible(true);
						column.append(",");
						column.append(f.getName());
						columnValue.append(",");
						columnValue.append(" ? ");
						list.add(f.get(entry));
					}
				}
			} else {
				for (Field f : field) {
					f.setAccessible(true);
					column.append(",");
					column.append(f.getName());
					columnValue.append(",");
					columnValue.append(" ? ");
					list.add(f.get(entry));
				}
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			System.out.println("得到实体插入语句时出现错误！" + e.getMessage());
		}
		sql.append(" INSERT INTO ");
		sql.append(getTable(entry.getClass()));
		sql.append(" ( ");
		sql.append(column.toString().substring(1));
		sql.append(" ) ");
		sql.append(" value ( ");
		sql.append(columnValue.toString().substring(1));
		sql.append(" ); ");
		map.put(sql.toString(), list);
		return map;
	}

	/**
	 * 生成删除语句
	 * 
	 * @param entryClass
	 * @return
	 */
	public static <T> String getDeleteSql(Class<T> entryClass) {
		StringBuilder sql = new StringBuilder();
		sql.append(" DELETE FROM ");
		sql.append(getTable(entryClass));
		sql.append(" WHERE ");
		sql.append(getId(entryClass));
		sql.append(" =? ");
		return sql.toString();
	}

	/**
	 * 生成删除语句
	 * 
	 * @param entryClass
	 * @param columnName
	 * @return
	 */
	public static <T> String getDeleteSql(Class<T> entryClass, String columnName) {
		StringBuilder sql = new StringBuilder();
		sql.append(" DELETE FROM ");
		sql.append(getTable(entryClass));
		sql.append(" WHERE ");
		sql.append(columnName);
		sql.append(" =? ");
		return sql.toString();
	}

	/**
	 * 生成更新语句
	 * 
	 * @param entry
	 * @return
	 */
	public static <T> Map<String, List<Object>> getUpdateSql(Object entry) {
		Map<String, List<Object>> sqlMap = new HashMap<String, List<Object>>();
		List<Object> list = new ArrayList<Object>();
		Field[] field = entry.getClass().getDeclaredFields();
		StringBuilder sql = new StringBuilder();
		StringBuilder parm = new StringBuilder();
		String IdName = null;
		Object IdValue = null;
		sql.append(" UPDATE ");
		sql.append(getTable(entry.getClass()));
		sql.append(" SET ");
		try {
			for (Field f : field) {
				f.setAccessible(true);
				if (f.isAnnotationPresent(IdMapping.class)) {
					IdName = f.getName();
					IdValue = f.get(entry);
				}
				if (!f.isAnnotationPresent(IdMapping.class)) {
					parm.append(", ");
					parm.append(f.getName());
					parm.append(" = ");
					parm.append(" ? ");
					list.add(f.get(entry));
				}
			}
		} catch (IllegalArgumentException | IllegalAccessException e) {
			System.out.println("得到实体更新语句出现错误！" + e.getMessage());
		}
		list.add(IdValue);
		sql.append(parm.toString().substring(1));
		sql.append(" where ");
		sql.append(IdName);
		sql.append(" = ");
		sql.append(" ? ");
		sqlMap.put(sql.toString(), list);
		return sqlMap;
	}

}
